package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.player.PlayerPotentialDaoImpl;
import com.yanqu.road.entity.player.PlatformUserPotential;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.web.controller.BaseJsonHttpServlet;
import org.springframework.boot.autoconfigure.session.SessionProperties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

@WebServlet(urlPatterns = "/player/potential")
public class ReceivePlayerPotential extends BaseJsonHttpServlet implements Serializable {
    private static final long serialVersionUID = -6665433544658238L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestContent = getRequestContent(request);
        List<PlatformUserPotential> potentialList = JSON.parseArray(requestContent, PlatformUserPotential.class);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ret", 0);
        jsonObject.put("msg", "success");
        for (PlatformUserPotential potential : potentialList) {
            try {
                JSONObject obj = new JSONObject();
                obj.put("platformUserId", potential.getPlatformUserId());
                obj.put("potential", potential.getPotential());
                RedisManger.getRedisUtil().setString("POTENTIAL-" + potential.getPlatformUserId(), JSON.toJSONString(obj));
                boolean success = new PlayerPotentialDaoImpl().add(potential);
                if (!success) {
                    jsonObject.put("ret", 1);
                    jsonObject.put("msg", "failure");
                }
            } catch (Exception e) {

            }
        }

        sendMessage(response,jsonObject.toJSONString());
    }

}
